home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 4 code / Poly. in Code Resources / Virtual WDEF / FixTables.cp < prev    next >
Encoding:
Text File  |  1990-07-24  |  3.3 KB  |  136 lines  |  [TEXT/MPS ]

  1. /*
  2.     FixTables.cp
  3.     
  4.     An MPW Tool to fix CFront's virtual tables in order to 
  5.     have them initialized at run-time.
  6.     
  7.     by Patrick Beard.
  8.     
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <Types.h>
  15.  
  16. char line[512];            // a line of input.
  17. char temp[512];            // temporary buffer.
  18. char className[512];    // the current class name (mangled).
  19.  
  20. void MIFixTables()
  21. {
  22.     char *table_starts = "struct __mptr __vtbl__";    // begins each table declaration.
  23.     int starts_match_len = strlen(table_starts);
  24.     char *table_ends = "0,0,0};";
  25.     int ends_match_len = strlen(table_ends);
  26.     int index;
  27.     int found_table = 0;
  28.     char *token, *s, *d;
  29.     
  30.     // begin the function.
  31.     fprintf(stdout, "void init_vtbls(void)\n{\n");
  32.     
  33.     while(fgets(line, sizeof(line), stdin)) {
  34.         if(!found_table) {
  35.             if(strncmp(table_starts, line, starts_match_len) == 0) {
  36.                 // we've started parsing a table.
  37.                 s = &line[starts_match_len]; d = className;
  38.                 while( *s != '[') *d++ = *s++;
  39.                 *d = 0;
  40.                 fprintf(stdout, "/* className = %s */\n", className);                
  41.                 index = 1;
  42.                 found_table = 1;
  43.             }
  44.         } else {
  45.             // we're processing a table.
  46.             if(strncmp(table_ends, line, ends_match_len) != 0) {
  47.                 token = strtok(line, ",");
  48.                 if(strcmp(token, "0") != 0)
  49.                     fprintf(stdout, "__vtbl__%s[%d].d = %s;\n", className, index, token);
  50.                 token = strtok(NULL, ",");
  51.                 if(strcmp(token, "0") != 0)
  52.                     fprintf(stdout, "__vtbl__%s[%d].i = %s;\n", className, index, token);
  53.                 token = strtok(NULL, ",");    // this token points to entry we want.
  54.                 fprintf(stdout, "__vtbl__%s[%d].f = %s;\n", className, index++, token);
  55.             } else {
  56.                 fprintf(stdout, "__ptbl__%s = __vtbl__%s;\n", className, className);
  57.                 found_table = 0;    // start over and search for another table.
  58.             }
  59.         }
  60.     }
  61.     
  62.     // finish the function off.
  63.     fprintf(stdout, "}\n");    
  64. }
  65.  
  66. void SIFixTables()
  67. {
  68.     char *table_starts = "__vptp __vtbl__";    // begins each table declaration.
  69.     int starts_match_len = strlen(table_starts);
  70.     char *table_ends = "0};";
  71.     int ends_match_len = strlen(table_ends);
  72.     int index;
  73.     int found_table = 0;
  74.     char *token, *s, *d;
  75.     
  76.     // begin the function.
  77.     fprintf(stdout, "void init_vtbls(void)\n{\n");
  78.     
  79.     while(fgets(line, sizeof(line), stdin)) {
  80.         if(!found_table) {
  81.             if(strncmp(table_starts, line, starts_match_len) == 0) {
  82.                 // we've started parsing a table.
  83.                 s = &line[starts_match_len]; d = className;
  84.                 while( *s != '[') *d++ = *s++;
  85.                 *d = 0;
  86.                 fprintf(stdout, "/* className = %s */\n", className);                
  87.                 index = 1;
  88.                 found_table = 1;
  89.             }
  90.         } else {
  91.             // we're processing a table.
  92.             if(strncmp(table_ends, line, ends_match_len) != 0) {
  93.                 token = strtok(line, ",");
  94.                 fprintf(stdout, "__vtbl__%s[%d] = %s;\n", className, index++, token);
  95.             } else {
  96.                 fprintf(stdout, "__ptbl__%s = __vtbl__%s;\n", className, className);
  97.                 found_table = 0;    // start over and search for another table.
  98.             }
  99.         }
  100.     }
  101.     
  102.     // finish the function off.
  103.     fprintf(stdout, "}\n");    
  104. }
  105.  
  106.  
  107. Boolean UsingMultipleInheritance(int argc, char** argv)
  108. {
  109.     // by default, we chew on the tables assuming multiple inheritance.
  110.     Boolean useMI = true;
  111.     while(--argc) {
  112.         char *arg = *++argv;
  113.         if(arg[0] == '-') {
  114.             switch(tolower(arg[1])) {
  115.             case 'm':
  116.                 useMI = true;
  117.                 break;
  118.             case 's':
  119.                 useMI = false;
  120.                 break;
  121.             }
  122.         }
  123.     }
  124.     
  125.     return useMI;
  126. }
  127.  
  128. int main(int argc, char** argv)
  129. {    
  130.     if(UsingMultipleInheritance(argc, argv))
  131.         MIFixTables();
  132.     else
  133.         SIFixTables();
  134.     return 0;
  135. }
  136.